package com.empsys;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;

import java.io.FileInputStream;
import java.io.InputStream;

/**
 * @author wzm
 * @create 2023-12-11 15:36
 * project_name:web2310
 * @Description
 */
public class OssDemo {
    private static CredentialsProvider credentialsProvider;

    public static void main(String[] args) throws Exception {
        /*
        * 以下代码中需要替换称为自己的的内容有：
        * accesskKeyId 阿里云账号访问的key
        * accessKeySecret 阿里云账号AccessKey访问的对应秘钥
        * endpoint 地域节点所在的位置
        * bucketName 存储空间的名字
        * filePath 上传文件的路径
        *objectName 存储的对象名，也就是上传到oss中存储的文件的名称
        *
        * */


        //地域节点   Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
        String endpoint = "https://oss-cn-beijing.aliyuncs.com";

        //测试代码 直接指定
        // STS临时访问密钥AccessKey ID和AccessKey Secret。
        String accessKeyId = "LTAI5tCrrrEbpZsfkbMqzYpV";
        String accessKeySecret = "m50vxFldhYG20G0GpgSRsckXGhC0XO";
        // 使用代码嵌入访问密钥和Secret。
        CredentialsProvider credentialsProvider = new DefaultCredentialProvider(accessKeyId, accessKeySecret);


        // 填写Bucket名称，例如examplebucket。
        String bucketName = "web2310-pro";

        // 填写Object完整路径，完整路径中不能包含Bucket名称，例如exampledir/exampleobject.txt。
        String objectName = "119.png"; //对象名称也就是存储在容器中的文件名字

        // 填写本地文件的完整路径，例如D:\\localpath\\examplefile.txt。
        // 如果未指定本地路径，则默认从示例程序所属项目对应本地路径中上传文件流。
        String filePath= "C:\\Users\\wzm\\Desktop\\值班.png";  //哪个文件

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);

        try {
            InputStream inputStream = new FileInputStream(filePath);
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
            // 创建PutObject请求。
            PutObjectResult result = ossClient.putObject(putObjectRequest);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}
